import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/back_button_widget.dart';

/// 章节练习标题组件
class PageTitleWidget extends StatelessWidget {
  /// 标题名称
  final String name;

  /// 页面上下文
  final BuildContext pageContext;

  /// 点击方法
  final Function()? onTapFun;

  /// 右边组件对应方法
  final Function()? rightFun;

  /// 右边组件是否显示，默认：false(不显示)
  final bool? rightShow;

  /// 右边组件边距，默认：px24
  final double? rightMargin;

  /// 右边组件自定义，默认：垃圾桶图标
  final Widget? rightChild;

  /// 背景色，默认：白色
  final Color? backgroundColor;

  const PageTitleWidget({
    super.key,
    required this.name,
    required this.pageContext,
    this.onTapFun,
    this.rightFun,
    this.rightShow = false,
    this.rightMargin,
    this.rightChild,
    this.backgroundColor,
  });

  /// 右边图标
  get _icon => GestureDetector(
        onTap: () {

          if (rightShow!) {
            rightFun!();
          }
        },
        child: rightChild ??
            SvgPicture.asset(
              "images/subject/subject_trash_can.svg",
              width: 24,
              height: 24,
            ),
      );

  @override
  Widget build(BuildContext context) {
    // 获取顶部安全边距
    return Container(
      height: 68 ,
      decoration: BoxDecoration(color: backgroundColor ?? colorWhite1),
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          BackButtonWidget.back(
            pageContext: pageContext,
            onTapFun: onTapFun,
            size: 26,
          ),
          Container(
            width: 120,
            child: Text(
              name,
              style: FontUtil.fontBlack1(22, fontWeight: FontWeight.w500),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          if (rightShow!) _icon else Container(),
        ],
      ),
    );
  }

}
